/**
 * Copyright(c) 2010 Ceno Techonologies Co., Ltd.
 *
 * History:
 *   15-7-14 下午4:54 Created by piaohh
 */
package com.jade.bss.beacon.apply.impl;

import java.util.List;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.jade.bss.beacon.apply.Apply;
import com.jade.bss.beacon.apply.ApplyDao;
import com.jade.framework.base.criterion.Criteria;
import com.jade.framework.base.criterion.sql.SqlUtils;
import com.jade.framework.base.util.PagingList;

/**
 *
 * @author <a href="mailto:Administrator@ceno.cn">piaohh</a>
 * @version 1.0 15-7-14 下午4:54
 */
@Named ("bss_applyDao")
public class JpaApplyDao implements ApplyDao
{
    @PersistenceContext (unitName = "default")
    protected EntityManager entityManager;

    public Apply get(long id)
    {
        return entityManager.find(Apply.class,id);
    }

    public void add(Apply apply)
    {
        entityManager.persist(apply);
    }

    public PagingList<Apply> query(Criteria criteria)
    {
        String sql="SELECT apply FROM Apply apply";
        return SqlUtils.sqlQuery(sql, entityManager, criteria);
    }

    public void update(long applyId, int status,String comment)
    {
        String sql = "UPDATE Apply set status=:status,comment=:comment WHERE id=:applyId";
        Query query = entityManager.createQuery(sql);
        query.setParameter("status",status);
        query.setParameter("comment",comment);
        query.setParameter("applyId",applyId);
        query.executeUpdate();
    }

    public List<Apply> list(Criteria criteria)
    {
        String sql="SELECT apply FROM Apply apply";
        return SqlUtils.sqlQueryWithoutCount(sql, entityManager, criteria);
    }
}
